package trucaso;
/*
 *  Panel to keep the scores.
 */
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JPanel;

public class ScoreBoard extends JPanel {
	int score1,score2;
	protected String name1,name2;
	protected BufferedImage[] matches;

	public ScoreBoard() {
		super();
		setBackground(new Color(0,2,129));
		
		name1 = "Player 1";
		name2 = "Player 2";

		score1 = 0;
		score2 = 0;
		
		matches = new BufferedImage[5];
		loadMatches();
	}
	
	protected void loadMatches() {
		try {
			for(int i=1;i<6;i++) {
				java.net.URL url = Deck.class.getResource("matches/"+i+".gif");
				matches[i-1] = ImageIO.read(url);
			}
		}catch(IOException ioex) {	}
	}
	
	protected void setNames(String n1,String n2) {
		if(n1==null) return;
		if(n2==null) return;
		name1 = n1;
		name2 = n2;
		repaint();
	}
	
	protected void setScores(int s1,int s2) {
		score1 = s1;
		score2 = s2;
	}
	
	
	protected void paintNames(Graphics2D g) {
		int width = getSize().width;
		g.setColor(Color.YELLOW);
		g.drawString(name1,10,13);
		g.drawString(name2,width/2+10,13);
		g.drawLine(width/2, 0, width/2, getSize().height);
		g.drawLine(0,18, width, 18);
	}
	
	protected void paintScore(Graphics2D g,int sc, int x) {
		if(sc==0) return;
		int diff = sc%5;
		int width = getSize().width/4;
		
		if(sc<5) {
			g.drawImage(matches[diff-1],x,20,width,width,null);
		}else {
			if(sc<10) {
				g.drawImage(matches[4],x,20,width,width,null);
				g.drawImage(matches[diff-1],x,20+width,width,width,null);
			}else {
				if(sc<15) {
					g.drawImage(matches[4],x,20,width,width,null);
					g.drawImage(matches[4],x,20+width,width,width,null);
					g.drawImage(matches[diff-1],x,20+width*2,width,width,null);
				}else{
					if(sc<20) {
						g.drawImage(matches[4],x,20,width,width,null);
						g.drawImage(matches[4],x,20+width,width,width,null);
						g.drawImage(matches[4],x,20+width*2,width,width,null);
						if(sc!=15){
							g.drawImage(matches[diff-1],x+width,20,width,width,null);
						}
					}else {
						if(sc<25) {
							g.drawImage(matches[4],x,20,width,width,null);
							g.drawImage(matches[4],x,20+width,width,width,null);
							g.drawImage(matches[4],x,20+width*2,width,width,null);
							g.drawImage(matches[4],x+width,20,width,width,null);
							if(sc!=20) {
								g.drawImage(matches[diff-1],x+width,20+width,width,width,null);
							}
						}else {
							if(sc<30) {
								g.drawImage(matches[4],x,20,width,width,null);
								g.drawImage(matches[4],x,20+width,width,width,null);
								g.drawImage(matches[4],x,20+width*2,width,width,null);
								g.drawImage(matches[4],x+width,20,width,width,null);
								g.drawImage(matches[4],x+width,20+width,width,width,null);
								if(sc!=25) {
									g.drawImage(matches[diff-1],x+width,20+width*2,width,width,null);
								}
							}else {
								if(sc==30) {
									g.drawImage(matches[4],x,20,width,width,null);
									g.drawImage(matches[4],x,20+width,width,width,null);
									g.drawImage(matches[4],x,20+width*2,width,width,null);
									g.drawImage(matches[4],x+width,20,width,width,null);
									g.drawImage(matches[4],x+width,20+width,width,width,null);
									g.drawImage(matches[4],x+width,20+width*2,width,width,null);
								}
							}
						}
					}
				}
			}
		}
	}
	
	protected void paintScores(Graphics2D g) {	
		paintScore(g,30,0);
		paintScore(g,30,getSize().width/2);
	}

	protected void paintComponent(Graphics arg0) {
		super.paintComponent(arg0);
		Graphics2D g = (Graphics2D)arg0;
		
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);

		paintNames(g);
		paintScores(g);
	}

}
